<template>
  <div>
    <el-upload
      action="https://knowledge.nervenets.com/api/v1/file/upload"
      list-type="picture-card"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :show-file-list="false"
      name="userfile"
    >
        <img style="margin-top: -1px;margin-left:-1px;width: 150px;height: 150px;border-radius: 6px" v-if="myDialogImageUrl"  :src="myDialogImageUrl" class="avatar">
        <i v-if="!myDialogImageUrl" class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
export default {
  name: 'UploadImg',
  props: {
      setImg: {
        type: Function,
        default: null
      },
      dialogImageUrl:{
        type:String,
        default:''
      }
    },
  data() {
    return {
      myDialogImageUrl:this.dialogImageUrl
    };
  },
  methods: {
    handleRemove(file) {
      console.log(file);
    },
    handleAvatarSuccess(res){
      this.myDialogImageUrl=res.data
      this.setImg(res.data)
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!');
      }
      return  isLt2M;
    }
  },created() {
     setTimeout(()=>{
            this.myDialogImageUrl=this.dialogImageUrl
        },100)
  }
}
</script>
